package com.crm.controller.saling;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.crm.pojo.Back;
import com.crm.pojo.PlaningBack;
import com.crm.service.saling.BackService;
import com.crm.service.saling.CODetailsService;
import com.crm.service.saling.PlaningBackService;
import com.crm.vo.Check;
import com.crm.vo.PlaningBackClientContractUserInfoVO;
import com.crm.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.*;

/**
 * Created by dr5hx on 2018/8/26
 */
@Controller
public class PlaningBackController {
    @Autowired
    private PlaningBackService planingBackService;
    @Autowired
    private CODetailsService coDetailsService;
    @Autowired
    private BackService backService;

    /**
     * 请求合同的统计值是否于订单值一致
     */
    @PostMapping("getContractMoney")
    @ResponseBody
    public Check getContractMoney(Long contractId) {
        contractId = contractId == null ? 0 : contractId;
        BigDecimal cOdetailMoney = coDetailsService.getCodetailMoneyByContractId(contractId);
        BigDecimal planingBackMoney = planingBackService.countPlaningBackMoneyByContractId(contractId);
        return new Check(cOdetailMoney, planingBackMoney, cOdetailMoney.equals(planingBackMoney));
    }

    /**
     * 保存回款计划列表
     *
     * @param rows
     * @param contractId
     * @return
     */
    @PostMapping("savePlaningBackListByContractId")
    @ResponseBody
    public Result savePlaningBackListByContractId(String rows, Long contractId) {
        try {
            if (rows != null && contractId != null) {
                List<PlaningBack> list = new ArrayList<>();
                JSONArray array = JSONArray.parseArray(rows);
                for (Object o : array) {
                    JSONObject jsonObject = (JSONObject) o;
                    PlaningBack planingBack = new PlaningBack();
                    planingBack.setPlaningBackMoney(new BigDecimal(jsonObject.get("planingbackmoney").toString()));
                    planingBack.setFkPlaningBackOrderId(new BigDecimal(contractId.toString()));
                    planingBack.setPlaningBackIsDel("否");
                    planingBack.setPlaningBackIsBack("否");
                    planingBack.setPlaningBackTime(new Date());
                    list.add(planingBack);
                }
                planingBackService.savePlaningBackDataList(list);
                return Result.SUCCESS;
            } else {
                return new Result("add data failed");

            }
        } catch (Exception e) {
            return new Result("unexpected error occured");
        }

    }

    /**
     * 获取对应的计划回款的列表
     *
     * @param contractId
     * @return
     */
    @PostMapping("getPlaningBackByContractId")
    @ResponseBody
    public List<PlaningBack> getPlaningBackByContractId(Long contractId) {
        contractId = contractId == null ? 0 : contractId;
        try {
            return planingBackService.getPlaningBackByContractId(contractId);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 设置回款为已回款
     * 并生成回款记录
     *
     * @param planingBackId
     * @param rows
     * @return
     */
    @PostMapping("setPlaningBackMoneyAlreadyBack")
    @ResponseBody
    public Boolean setPlaningBackMoneyAlreadyBack(Long planingBackId, String rows) {
        try {
            if (planingBackId != null && rows != null) {
                planingBackId = planingBackId == null ? 0 : planingBackId;
                JSONObject jsonObject = JSON.parseObject(rows);
                Back back = new Back();
                back.setBackIsDel("否");
                PlaningBack planingBack = planingBackService.getPlaningBackRecord(planingBackId);
                back.setBackMoney(planingBack.getPlaningBackMoney());
                back.setBackTime(new Date());
                back.setFkBackPlaningBackId(new BigDecimal(jsonObject.get("planingBackId").toString()));
                if (planingBack.getPlaningBackIsBack().equals("否")) {
                    planingBack.setPlaningBackIsBack("是");
                    planingBackService.savePlaningBackData(planingBack);
                    backService.saveBackRecord(back);
                    return true;
                } else {
                    return false;
                }
            } else {
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 显示对应的合同的对应回款记录
     *
     * @param contractId
     * @return
     */
    @PostMapping("getBackRecordByContractId")
    @ResponseBody
    public List<Back> getBackRecordByContractId(Long contractId) {
        contractId = contractId == null ? 0 : contractId;
        try {
            return backService.getBackRecordByContractId(contractId);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 撤销回款记录
     *
     * @param backId
     * @return
     */
    @PostMapping("withdrawBackRecord")
    @ResponseBody
    public Boolean withdrawBackRecord(Long backId) {
        Back back = backService.getBackByBackId(backId);
        if (back != null) {
            PlaningBack planingBack = planingBackService.getPlaningBackRecord(back.getFkBackPlaningBackId().longValue());
            if (planingBack != null) {
                planingBack.setPlaningBackIsBack("否");
                back.setBackIsDel("是");
                return planingBackService.savePlaningBackData(planingBack) && backService.saveBackRecord(back)
                        != null;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    /**
     * 分页显示计划回款数据
     *
     * @param page
     * @param rows
     * @return
     */
    @PostMapping("getPlaningBackClientContractUserInfoVOList")
    @ResponseBody
    public Map getPlaningBackClientContractUserInfoVOList(int page, int rows) {
        try {
            List<PlaningBackClientContractUserInfoVO> list = planingBackService.getPlaningBackClientContractUserInfoVOList(page, rows);
            Long count = planingBackService.getPlaningBackCount();
            Map map = new HashMap();
            map.put("rows", list);
            map.put("total", count);
            return map;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 滚去计划回款
     *
     * @return
     */
    @GetMapping("toPlaningBackClientContractUserInfoVOList")
    public String toPlaningBackClientContractUserInfoVOList() {
        return "saling/planningBackList";
    }

}
